var $$ = function(a) {
    return "string" == typeof a ? document.getElementById(a) : a
};
var Class = {
    create: function() {
        return function() {
            this.initialize.apply(this, arguments)
        }
    }
};
Object.extend = function(a, c) {
    for (var b in c) {
        a[b] = c[b]
    }
    return a
}
;
var Calendar = Class.create();
Calendar.prototype = {
    initialize: function(a, b) {
        this.Container = $$(a);
        this.Days = [];
        this.SetOptions(b);
        this.Year = this.options.Year;
        this.Month = this.options.Month;
        this.onToday = this.options.onToday;
        this.onSignIn = this.options.onSignIn;
        this.onFinish = this.options.onFinish;
        this.qdDay = this.options.qdDay;
        this.isSignIn = false;
        this.Draw()
    },
    SetOptions: function(a) {
        this.options = {
            Year: new Date().getFullYear(),
            Month: new Date().getMonth() + 1,
            qdDay: null,
            onToday: function() {},
            onSignIn: function() {},
            onFinish: function() {}
        };
        Object.extend(this.options, a || {})
    },
    PreMonth: function() {
        var a = new Date(this.Year,this.Month - 2,1);
        this.Year = a.getFullYear();
        this.Month = a.getMonth() + 1;
        this.Draw()
    },
    NextMonth: function() {
        var a = new Date(this.Year,this.Month,1);
        this.Year = a.getFullYear();
        this.Month = a.getMonth() + 1;
        this.Draw()
    },
    Draw: function() {
        var e = this.qdDay;
        var a = [];
        for (var h = 1, f = new Date(this.Year,this.Month - 1,1).getDay(); h <= f; h++) {
            a.push("&nbsp;")
        }
        for (var h = 1, k = new Date(this.Year,this.Month,0).getDate(); h <= k; h++) {
            a.push(h)
        }
        var g = document.createDocumentFragment();
        this.Days = [];
        while (a.length > 0) {
            var l = document.createElement("tr");
            for (var h = 1; h <= 7; h++) {
                var b = document.createElement("td");
                b.innerHTML = "&nbsp;";
                if (a.length > 0) {
                    var c = a.shift();
                    b.innerHTML = "<span>" + c + "</span>";
                    if (c > 0 && e.length) {
                        for (var j = 0; j < e.length; j++) {
                            this.Days[c] = b;
                            if (this.IsSame(new Date(this.Year,this.Month - 1,c), e[j])) {
                                this.onToday(b)
                            }
                            if (this.checkSignIn(new Date(), e[j])) {
                                this.onSignIn()
                            }
                        }
                    }
                }
                l.appendChild(b)
            }
            g.appendChild(l)
        }
        while (this.Container.hasChildNodes()) {
            this.Container.removeChild(this.Container.firstChild)
        }
        this.Container.appendChild(g);
        this.onFinish();
        if (this.isSignIn) {
            this.isSignIn = false;
            return this.SignIn()
        }
    },
    IsSame: function(a, b) {
        b = new Date(b * 1000);
        return (a.getFullYear() == b.getFullYear() && a.getMonth() == b.getMonth() && a.getDate() == b.getDate())
    },
    checkSignIn: function(a, b) {
        b = new Date(b * 1000);
        return (a.getFullYear() == b.getFullYear() && a.getMonth() == b.getMonth() && a.getDate() == b.getDate())
    },
    querySign: function(date){
    	var a = new Date(date);
        this.Year = a.getFullYear();
        this.Month = a.getMonth() + 1;
        this.Draw()
    },
    SignIn: function() {
        var f = new Date();
        var h = f.getFullYear();
        var e = f.getMonth() + 1;
        if (h != this.Year || e != this.Month) {
            this.Year = h;
            this.Month = e;
            this.isSignIn = true;
            return this.Draw()
        }
        var b = f.getDate();
        var a = new Array();
        var g = document.getElementById("idCalendar");
        for (var c = 0; c < g.rows.length; c++) {
            for (var d = 0; d < g.rows[c].cells.length; d++) {
                if (b == g.rows[c].cells[d].innerText && h == this.Year && e == this.Month) {
                    if (g.rows[c].cells[d].className == "onToday") {
                        return 2
                    }
                    g.rows[c].cells[d].className = "onToday";
                    this.qdDay.push(Date.parse(new Date()) / 1000);
                    return 1
                }
            }
        }
    }
};